<template>
    <form>
        <slot></slot>
    </form>
</template>

<script>
    export default {
        provide() {
            return {
                // 将表单实力传递给后代
                form: this
            };
        },
        props: {
            model: {
                type: Object,
                required: true
            },
            rules: {
                type: Object
            }
        },
        created() {
            // 需要把所有的校验的 表单项 放在一个地方
            this.fields = [];
            this.$on('formItemAdd', item => this.fields.push(item));
        },
        methods: {
            async validate(callback) {
                const tasks = this.fields.map(item => item.validate());

                const results = await Promise.all(tasks);
                let ret = true;
                results.forEach(valid => {
                    if (!valid) {
                        ret = false
                    }
                });
                callback(ret);
            }
        },
    }
</script>

<style scoped>

</style>